package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.base.page.TableHelper;
import com.lam.common.utils.StringUtils;
import com.lam.mall.app.vo.NoticeVo;
import com.lam.mall.entity.Notice;
import com.lam.mall.mapper.NoticeMapper;
import com.lam.mall.param.NoticeParam;
import com.lam.mall.service.NoticeService;

/**
 * 公告管理Service业务层处理
 * 
 * @author admin
 * @date 2023-04-20
 */
@Service
public class NoticeServiceImpl  implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 查询公告管理
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public Notice selectById(String id) {

        return noticeMapper.selectById(id);
    }

    /**
     * 查询公告管理列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<Notice> selectList(NoticeParam param) {
    
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("title", param.getKeywords());
            });
        }
        
        queryWrapper.eq(Objects.nonNull(param.getShopId()), "shop_id", param.getShopId());
        queryWrapper.like(StringUtils.isNotBlank(param.getTitleKey()), "title", param.getTitleKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTitle()), "title", param.getTitle());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.ge(Objects.nonNull(param.getPublishTimeBegin()), "publish_time", param.getPublishTimeBegin());
        queryWrapper.le(Objects.nonNull(param.getPublishTimeEnd()), "publish_time", param.getPublishTimeEnd());
        

        return noticeMapper.selectList(queryWrapper);
    }

    /**
     * 新增公告管理
     * 
     * @param notice 
     * @return 
     */
    @Override
    @Transactional
    public int insert(Notice notice) {
    
        return noticeMapper.insert(notice);
    }

    /**
     * 修改公告管理
     * 
     * @param notice 
     * @return 
     */
    @Override
    @Transactional
    public int update(Notice notice) {
    
        return noticeMapper.updateById(notice);
    }

    /**
     * 删除公告管理信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return noticeMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Override
    @Cacheable(cacheNames = "notices", key = "'notices'")
    public List<Notice> listNoticeCache() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus, 1)
                .eq(Notice::getIsTop, 1)
                .orderByDesc(Notice::getPublishTime));
    }

    @Override
    @CacheEvict(cacheNames = "notices", key = "'notices'")
    public void removeNoticeCache() {
    }

    @Override
    public List<NoticeVo> pageNotice() {
    	TableHelper.startPage();
        return noticeMapper.pageNotice();
    }

    @Override
    @Cacheable(cacheNames = "notice", key = "#noticeId")
    public Notice getNoticeByIdCache(String noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    @CacheEvict(cacheNames = "notice", key = "#noticeId")
    public void removeNoticeByIdCache(String noticeId) {
    }
    
}
